/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal_rticonnextdds/config_manager/ConfigManager.hpp"
#include "ndds/ndds_c.h"

ConfigManager* ConfigManager::GetInstance()
{
    static ConfigManager instance;
    return &instance;
}

bool ConfigManager::loadConfigProfileByEnv()
{
    const char* path = getenv(DAL_CONFIG_FILE_PATH_ENV);
    if (!path || std::string(path).empty())
        return false;

    std::string pathStr = path;

    if (pathStr.empty()) {
        LOG_INFO() << "[configloader - env mode] env " << DAL_CONFIG_FILE_PATH_ENV << " is empty."
                   << LOG_ENDL;
        LOG_INFO() << "[configloader - code mode] success, "
                   << "no config file take effects." << LOG_ENDL;
        clearSetting();
        return false;
    }

    if (access(pathStr.c_str(), F_OK) != 0) {
        LOG_INFO() << "[configloader - env mode] config file " << pathStr << " not exist."
                   << LOG_ENDL;
        LOG_INFO() << "[configloader - code mode] success, "
                   << "no config file take effects." << LOG_ENDL;
        clearSetting();
        return false;
    }

    if (setenv("NDDS_USER_QOS_SET_FLAG", "1", 1) != 0) {
        LOG_INFO() << "[configloader - env mode] NDDS_QOS_PROFILES env set fail." << LOG_ENDL;
        LOG_INFO() << "[configloader - code mode] success, "
                   << "no config file take effects." << LOG_ENDL;
        clearSetting();
        return false;
    }

    DDS_DomainParticipantFactoryQos* r_factory_qos = new DDS_DomainParticipantFactoryQos();
    DDS_DomainParticipantFactory* factory = DDS_DomainParticipantFactory_get_instance();
    DDS_DomainParticipantFactory_get_qos(factory, r_factory_qos);

    const char* url_profiles[1] = {path};
    DDS_StringSeq_from_array(&r_factory_qos->profile.url_profile, url_profiles, 1);
    DDS_DomainParticipantFactory_set_qos(factory, r_factory_qos);

    setConfigType(CONFIG_TYPE_SET_BY_ENV);
    setRtiddsProfilePath(pathStr);
    LOG_INFO() << "[configloader - env mode] success, "
               << "config file [" << pathStr << "] takes effect." << LOG_ENDL;
    return true;
}

void ConfigManager::setConfigType(ConfigManager::ConfigType type)
{
    config_type_ = type;
}

ConfigManager::ConfigType ConfigManager::getConfigType()
{
    return config_type_;
}

void ConfigManager::setRtiddsProfilePath(const std::string path)
{
    rtidds_profile_path_ = path;
}

const std::string ConfigManager::getRtiddsProfilePath()
{
    return rtidds_profile_path_;
}

void ConfigManager::clearSetting()
{
    rtidds_profile_path_ = "";
    config_type_ = CONFIG_TYPE_NOT_SET;
}
